package com.aier.proxy.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.entrance.common.EntranceICFinalData;
import com.aier.entrance.common.EntranceServerData;
import com.aier.proxy.entity.VideoDeviceInfo;

public class VideoDeviceInfoRowMapper implements RowMapper<VideoDeviceInfo> {

    @SuppressWarnings("unused")
    private int type;

    public VideoDeviceInfoRowMapper() {
    }

    public VideoDeviceInfoRowMapper(int type) {
        this.type = type;
    }

    @Override
    public VideoDeviceInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
        return parseVideoDevice(new VideoDeviceInfo(), rs);
    }

    public static VideoDeviceInfo parseVideoDevice(VideoDeviceInfo vd, ResultSet rs)
        throws SQLException {
        return parseVideoDevice(vd, rs, null);
    }

    public static VideoDeviceInfo parseVideoDevice(VideoDeviceInfo vd, ResultSet rs,
            String su) throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            vd.setVid(rs.getLong(su + "vid"));
        } catch (SQLException e) {
            vd.setVid(null);
        }
        try {
            vd.setSn(rs.getString(su + "sn"));
        } catch (SQLException e) {
            vd.setSn(null);
        }
        try {
            vd.setState(rs.getShort(su + "state"));
        } catch (SQLException e) {
            vd.setState(null);
        }
        try {
            vd.setUser(rs.getString(su + "user"));
        } catch (Exception e) {
            vd.setUser(null);
        }
        try {
            vd.setPwd(rs.getString(su + "pwd"));
        } catch (Exception e) {
            vd.setPwd(null);
        }
        try {
            vd.setUrl(rs.getString(su + "url"));
        } catch (Exception e) {
            vd.setUrl(null);
        }
        try {
            vd.setSupplier(rs.getShort(su + "supplier"));
        } catch (Exception e) {
            vd.setSupplier(null);
        }
        try {
            vd.setPid(rs.getInt(su + "pid"));
        } catch (Exception e) {
            vd.setPid(null);
        }
        try {
            vd.setIp(rs.getString(su + "ip"));
        } catch (Exception e) {
            vd.setIp(null);
        }
        try {
            vd.setPort(rs.getInt(su + "port"));
        } catch (Exception e) {
            vd.setPort(null);
        }
        if (vd.getState() == EntranceICFinalData.VIDEO_STATE_ON) {
            vd.setStateStr("启用");
        } else if (vd.getState() == EntranceICFinalData.VIDEO_STATE_OFF) {
            vd.setStateStr("停用");
        } else {
            vd.setStateStr("报废");
        }
        vd.setSupplierStr(EntranceServerData.videoSupplier.get(vd.getSupplier()));
        return vd;
    }

    public static Map<String, Object> getPar(Map<String, Object> par,
            VideoDeviceInfo vd) {
        par.put("vid", vd.getVid());
        par.put("sn", vd.getSn());
        par.put("state", vd.getState());
        par.put("user", vd.getUser());
        par.put("pwd", vd.getPwd());
        par.put("url", vd.getUrl());
        par.put("supplier", vd.getSupplier());
        par.put("pid", vd.getPid());
        par.put("ip", vd.getIp());
        par.put("port", vd.getPort());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args,
            VideoDeviceInfo vd) {
        args.put("vid", vd.getVid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final VideoDeviceInfo vd) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(vd),
                        new String[] { "vid" });
                parsePS(ps, vd);
                return ps;
            }

        };
    }

    private static String parseSQL(VideoDeviceInfo vd) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into video_device(");
        int count = 0;
        if (null != vd.getSn()) {
            sql.append("sn,");
            count++;
        }

        if (null != vd.getState()) {
            sql.append("state,");
            count++;
        }

        if (null != vd.getVid()) {
            sql.append("vid,");
            count++;
        }
        if (null != vd.getUser()) {
            sql.append("user,");
            count++;
        }
        if (null != vd.getPwd()) {
            sql.append("pwd,");
            count++;
        }
        if (null != vd.getUrl()) {
            sql.append("url,");
            count++;
        }
        if (null != vd.getSupplier()) {
            sql.append("supplier,");
            count++;
        }
        if (null != vd.getPid()) {
            sql.append("pid,");
            count++;
        }
        if (null != vd.getIp()) {
            sql.append("ip,");
            count++;
        }
        if (null != vd.getPort()) {
            sql.append("port,");
            count++;
        }

        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, VideoDeviceInfo vd)
        throws SQLException {
        int count = 1;
        if (null != vd.getSn()) {
            ps.setString(count++, vd.getSn());
        }

        if (null != vd.getState()) {
            ps.setShort(count++, vd.getState());
        }

        if (null != vd.getVid()) {
            ps.setLong(count++, vd.getVid());
        }

        if (null != vd.getUser()) {
            ps.setString(count++, vd.getUser());
        }
        if (null != vd.getPwd()) {
            ps.setString(count++, vd.getPwd());
        }
        if (null != vd.getUrl()) {
            ps.setString(count++, vd.getUrl());
        }
        if (null != vd.getSupplier()) {
            ps.setShort(count++, vd.getSupplier());
        }
        if (null != vd.getPid()) {
            ps.setInt(count++, vd.getPid());
        }
        if (null != vd.getIp()) {
            ps.setString(count++, vd.getIp());
        }
        if (null != vd.getPort()) {
            ps.setInt(count++, vd.getPort());
        }
    }

}
